/**
 * Created by huangjie on 17/3/27.
 */
$(function () {
    $("#jqGrid").jqGrid({
        url: '/tts/sys/user/list',
        datatype: "json",
        colModel: [
            { label: '用户ID', name: 'id', width: 60, key: true },
            { label: '姓名', name: 'name', width: 50 },
            { label: '用户名', name: 'userName', width: 60 },
            { label: '用户状态', name: 'status', width: 60, formatter: function(value, options, row){
                if(value === 'REGISTER_SUCCESS'){
                    return '<span class="label label-primary">注册成功</span>';
                }
                if(value === 'VERIFYING'){
                    return '<span class="label label-success">验证过程中</span>';
                }
                if(value === 'VERIFIED'){
                    return '<span class="label label-warning">验证成功</span>';
                }
                if(value === 'NOT_VERIFIED'){
                    return '<span class="label label-warning">未验证成功</span>';
                }
            }},
            { label: '用户类型', name: 'userType', width: 60, formatter: function(value, options, row){
                if(value === 'CUSTOMER'){
                    return '<span class="label label-primary">客户</span>';
                }
                if(value === 'DESIGNER'){
                    return '<span class="label label-success">设计师</span>';
                }
                if(value === 'EXPERT'){
                    return '<span class="label label-warning">专家</span>';
                }
                if(value === 'ADMIN'){
                    return '<span class="label label-warning">平台管理员</span>';
                }
                if(value === 'SUPER_ADMIN'){
                    return '<span class="label label-warning">平台超级管理员</span>';
                }
            }},
            { label: '角色', name: 'role.name', width: 50 },
            { label: '手机号码', name: 'mobileNumber', width: 50 },
            { label: '邮箱', name: 'email', width: 100 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});


var vm = new Vue({
    el:'#rrapp',
    mixins: [mixin],
    data:{
        showList: true,
        title: null,
        navTitle:"系统管理 > 用户管理",
        errorMsg:'',
        user:{
            userName: null, name:null,
            role:{
                id:null
            }
        },
        roleList : {

        },
        q:{
            name:null
        }
    },
    computed:{
        errorShow :function () {
            return this.errorMsg!='';
        }
    },
    created: function () {
        this.fetchData()
    },
    methods: {
        fetchData :function(){
            $.get("/tts/sys/role/list?limit=50", function(r){
                vm.roleList = r.page.list;
            });
        },
        search :function(){
            var json = getJSONObjectFromObject(vm.q);
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:json,
                page:page
            }).trigger("reloadGrid");
        },
        add: function(){
            vm.errorMsg=""
            vm.showList = false;
            vm.title = "新增";
            vm.user = {userName:null,name:null};
        },
        update: function (event) {
            var menuId = getSelectedRow();
            if(menuId == null){
                return ;
            }

            $.get("/tts/sys/user/info/"+menuId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.user = r.user;
            });
        },
        valid:function(event){
            var menuIds = getSelectedRows();
            if(menuIds == null){
                return ;
            }
            var ids = [];
            menuIds.forEach(function(e){
                ids.push({name:"id",value:e})
            })
            confirm('确定要启用选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "/tts/sys/user/valid",
                    data: ids,
                    contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        invalid: function (event) {
            var menuIds = getSelectedRows();
            if(menuIds == null){
                return ;
            }
            var ids = [];
            menuIds.forEach(function(e){
                ids.push({name:"id",value:e})
            })
            confirm('确定要禁用选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "/tts/sys/user/invalid",
                    data: ids,
                    contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            var url =  "/tts/sys/user/save"
            var json = getJSONArrayFromObject(vm.user);
            $.ajax({
                type: "POST",
                url: url,
                contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                data: json,
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});